package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.*;
import com.wn.class30.mapper.PayOrderDao;
import com.wn.class30.service.BackMyorderShareService;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@RestController
public class BackPayorderShareController {
    @Resource
    private BackMyorderShareService backMyorderShareService;
    @Resource
    private PayOrderDao payOrderDao;
    //管理员以门店为单位查询门店消费记录权限
    @RequestMapping("/houtai/page/finance/businesspay/sharewithbusiness/list")
    @RequiresPermissions("/houtai/page/finance/businesspay/sharewithbusiness/list")
    public Result getList()throws Exception{
        return new Result("200","success",null,null);
    }
    //管理员以门店为单位查询门店消费记录
    @RequestMapping("/houtai/finance/getBusinessPayFinances")
    public Result getBusinessPayShareByPage(Integer page,Integer limit,String businessName,String businessTel) throws Exception{
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf(page));
        map.put("limit",String.valueOf(limit) );
        map.put("businessName", businessName);
        map.put("businessTel", businessTel);
        Page pager = new Page();
        pager.setCurrentPage(page);
        pager.setPageSize(limit);
        int count = backMyorderShareService.selectCount((HashMap) map);
        pager.setTotalSize(count);
        List<Business> businesses = (List) backMyorderShareService.select((HashMap) map);
        List<PayorderShareCount> list = new ArrayList<>();
        for (Business business : businesses) {
            PayorderShareCount payorderShareCount = new PayorderShareCount();
            payorderShareCount.setBusinessId(business.getBusinessId());
            payorderShareCount.setBusinessName(business.getBusinessName());
            payorderShareCount.setBusinessTel(business.getBusinessTel());
            PayOrderExample ex = new PayOrderExample();
            PayOrderExample.Criteria criteria = ex.createCriteria();
            criteria.andBussinessIdEqualTo(business.getBusinessId());
            criteria.andPayOrderStateEqualTo("1");
            List<PayOrder> payorderShares = payOrderDao.selectByExample(ex);
            double totalMoney = 0;
            double unShareMoney = 0;
            for (PayOrder payOrder : payorderShares) {
                totalMoney += payOrder.getPayMoney();
            }
            payorderShareCount.setTotalMoney(totalMoney);
            criteria.andPayOrderSharestatusEqualTo(0);
            List<PayOrder> payorderShares2 = payOrderDao.selectByExample(ex);
            for (PayOrder payOrder : payorderShares2) {
                unShareMoney+= payOrder.getPayMoney();
            }
            payorderShareCount.setUnShareMoney(unShareMoney);
            list.add(payorderShareCount);
        }
        return new Result("0","success",pager, list);
    }
    //修改某商家所有未结算订单
    @RequestMapping("/houtai/page/finance/businesspay/updateAllByBusiId")
    @RequiresPermissions("/houtai/page/finance/businesspay/sharewithbusiness/update")
    public Result updatePayorderShareCount(Integer businessId)throws Exception{
        PayOrderExample ex = new PayOrderExample();
        PayOrderExample.Criteria criteria = ex.createCriteria();
        criteria.andBussinessIdEqualTo(businessId);
        criteria.andPayOrderStateEqualTo("1");
        List<PayOrder> li = payOrderDao.selectByExample(ex);
        for (PayOrder payOrder : li) {
            if(payOrder.getPayOrderSharestatus()==0){
                payOrder.setPayOrderSharestatus(1);
                payOrder.setPaySharetime(new Date());
                payOrderDao.updateByPrimaryKeySelective(payOrder);
            }

        }

        return new Result("0","success",null, null);
    }
    //byshareId修改结算状态
    @RequestMapping("/houtai/page/finance/businesspay/updateByPayorderShareId")
    @RequiresPermissions("/houtai/page/finance/businesspay/update")
    public Result updateByPayorderShareId(Integer pid)throws Exception{

        PayOrderExample ex = new PayOrderExample();
        PayOrderExample.Criteria criteria = ex.createCriteria();
        criteria.andPidEqualTo(pid);
        List<PayOrder> li = payOrderDao.selectByExample(ex);

        for (PayOrder payOrder : li) {

            if(payOrder.getPayOrderSharestatus()==0){

                payOrder.setPayOrderSharestatus(1);
                payOrder.setPaySharetime(new Date());
                payOrderDao.updateByPrimaryKeySelective(payOrder);
            }
        }

        return new Result("0","success",null, null);
    }

}
